import { lastValueFrom } from "rxjs";
import { Injectable } from "@nestjs/common";

import {
  Access,
  RestDto,
  ENUM_APP,
  AccessAll,
  putAccessRpcDto,
  AccessRpcInterface,
  searchAccessRpcDto,
  AccessRpcSearchPath,
  AccessRpcPutSyncPath,
  checkDroitContextRes,
  checkStockNodeDroitContextRpcDto,
  checkVoucherGroupDroitContextRpcDto,
  checkTonOrderGroupDroitContextRpcDto,
  AccessRpcCheckStockNodeDroitContextPath,
  AccessRpcCheckVoucherGroupDroitContextPath,
  AccessRpcCheckTonOrderGroupDroitContextPath,
} from "qqlx-core";

import { BusRpc } from "./app.droplet-bus";
import { RpcWaiting } from "./rpc.response";
import { RpcConnection } from "./com.connection";

@Injectable()
export class AccessRpc extends RpcConnection implements AccessRpcInterface<RestDto> {
  constructor(readonly BusRpc: BusRpc) {
    super(BusRpc, ENUM_APP.STREAM_ACCESS);
  }

  @RpcWaiting(AccessRpcSearchPath)
  async search(dto: RestDto & searchAccessRpcDto) {
    const ob = this.CLIENT.send(AccessRpcSearchPath, dto);
    return lastValueFrom<AccessAll>(ob);
  }

  @RpcWaiting(AccessRpcPutSyncPath)
  async putSync(dto: RestDto & putAccessRpcDto) {
    const ob = this.CLIENT.send(AccessRpcPutSyncPath, dto);
    return lastValueFrom<Access>(ob);
  }

  @RpcWaiting(AccessRpcCheckStockNodeDroitContextPath)
  async checkStockNodeDroitContext(dto: RestDto & checkStockNodeDroitContextRpcDto) {
    const ob = this.CLIENT.send(AccessRpcCheckStockNodeDroitContextPath, dto);
    return lastValueFrom<checkDroitContextRes>(ob);
  }

  @RpcWaiting(AccessRpcCheckTonOrderGroupDroitContextPath)
  async checkTonOrderGroupDroitContext(dto: RestDto & checkTonOrderGroupDroitContextRpcDto) {
    const ob = this.CLIENT.send(AccessRpcCheckTonOrderGroupDroitContextPath, dto);
    return lastValueFrom<checkDroitContextRes>(ob);
  }

  @RpcWaiting(AccessRpcCheckVoucherGroupDroitContextPath)
  async checkVoucherGroupDroitContext(dto: RestDto & checkVoucherGroupDroitContextRpcDto) {
    const ob = this.CLIENT.send(AccessRpcCheckVoucherGroupDroitContextPath, dto);
    return lastValueFrom<checkDroitContextRes>(ob);
  }
}
